package com.mindata.blockchain.demo;

import com.mindata.blockchain.block.*;
import com.mindata.blockchain.block.RLP.BlockB;
import com.mindata.blockchain.block.RLP.BlockBodyB;
import com.mindata.blockchain.block.RLP.BlockHeaderB;
import com.mindata.blockchain.block.RLP.InstructionB;
import com.mindata.blockchain.block.db.DbStore;
import com.mindata.blockchain.common.util.ByteUtil;
import com.mindata.blockchain.core.manager.DbBlockManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Created by Ming on 2018/8/10.
 */
@RestController
@RequestMapping("rlp")
public class RLPController {

    @Autowired
    DbBlockManager dbBlockManager;

    @Resource
    DbStore dbStore;
    @RequestMapping("/a")
    public Object saveRLp(){
        Instruction instruction=new Instruction();
        instruction.setHash("123");
        instruction.setSign("sign");
        instruction.setTimeStamp(System.currentTimeMillis());
        instruction.setJson("json");
        instruction.setPublicKey("公钥");
        instruction.setInstructionId("id");
        instruction.setOldJson("old");
        instruction.setOperation(Operation.ADD);
        instruction.setTable("table");
        InstructionB instructionB=new InstructionB();
        instructionB.setHash(instruction.getHash().getBytes());
        instructionB.setSign(instruction.getSign().getBytes());
        instructionB.setTimeStamp(ByteUtil.longToBytes(instruction.getTimeStamp()));
        instructionB.setJson(instruction.getJson().getBytes());
        instructionB.setPubliceKey(instruction.getPublicKey().getBytes());
        instructionB.setInstructionId(instruction.getInstructionId().getBytes());
        instructionB.setOldJson(instruction.getOldJson().getBytes());
        instructionB.setOperation(instruction.getOperation());
        instructionB.setTable(instruction.getTable().getBytes());
        String id= UUID.randomUUID().toString();
        dbStore.put(id.getBytes(),instructionB.encode());
        byte[] rs=dbStore.get(id.getBytes());
        InstructionB instructionB1=new InstructionB(rs);
        return instructionB1.convertToInstruction();
    }

    @RequestMapping("b")
    public Object kb() throws UnsupportedEncodingException {
        Block block=new Block();
        BlockHeader blockHeader=new BlockHeader();
        BlockBody blockBody=new BlockBody();
        block.setBlockHeader(blockHeader);
        block.setBlockBody(blockBody);
        List<String> list=new ArrayList<>();
        list.add("1");
        list.add("2");
        List<Instruction> insList=new ArrayList();
        insList.add(new Instruction());
        insList.add(new Instruction());
        block.getBlockHeader().setHashList(list);
        block.getBlockHeader().setHashMerkleRoot("HashMerkleRoot");
        block.getBlockHeader().setHashPreviousBlock("HashPreviousBlock");
        block.getBlockHeader().setNumber(123);
        block.getBlockHeader().setPublicKey("公钥");
        block.getBlockHeader().setTimeStamp(System.currentTimeMillis());
        block.getBlockHeader().setVersion(123);
        block.getBlockHeader().setNonce(999L);
        block.getBlockBody().setInstructions(insList);
        block.setHash("hash");

        BlockHeaderB blockHeaderB=new BlockHeaderB();
        BlockBodyB blockBodyB=new BlockBodyB();
        BlockB blockB=new BlockB();
        blockB.setBlockBodyB(blockBodyB);
        blockB.setBlockHeaderB(blockHeaderB);
        List<InstructionB> instructions=new ArrayList<>();
        InstructionB instructionB=new InstructionB();
        instructionB.setHash("hash".getBytes());
        instructions.add(instructionB);
        instructions.add(instructionB);
        blockB.getBlockBodyB().setInstructions(instructions);
        blockB.getBlockHeaderB().setHashList(new byte[block.getBlockHeader().getHashList().size()][]);
        for(int i=0;i<block.getBlockHeader().getHashList().size();i++)
            blockB.getBlockHeaderB().getHashList()[i]=block.getBlockHeader().getHashList().get(i).getBytes();
//        blockB.getBlockHeaderB().setHashList((byte[][]) block.getBlockHeader().getHashList().toArray());
        blockB.getBlockHeaderB().setNonce( ByteUtil.longToBytes(block.getBlockHeader().getNonce()));
        blockB.getBlockHeaderB().setHashMerkleRoot(block.getBlockHeader().getHashMerkleRoot().getBytes());
        blockB.getBlockHeaderB().setHashPreviousBlock(block.getBlockHeader().getHashPreviousBlock().getBytes());
        blockB.getBlockHeaderB().setNumber(ByteUtil.intToBytes(block.getBlockHeader().getNumber()));
        blockB.getBlockHeaderB().setTimeStamp(ByteUtil.longToBytes(block.getBlockHeader().getTimeStamp()));
        blockB.getBlockHeaderB().setVersion(ByteUtil.intToBytes(block.getBlockHeader().getVersion()));
        blockB.getBlockHeaderB().setPublicKey(block.getBlockHeader().getPublicKey().getBytes());
        blockB.setHash(block.getHash().getBytes());

        String id= UUID.randomUUID().toString();
        dbStore.put(id.getBytes(),blockB.encode());
        byte[] rs=dbStore.get(id.getBytes());
        BlockB blockB1=new BlockB(rs);
        return blockB1.convertToBlock();
    }

    @RequestMapping("c")
    public Object c(){
        Instruction instruction=new Instruction();
        instruction.setHash("123");
        instruction.setSign("sign");
        instruction.setTimeStamp(System.currentTimeMillis());
        instruction.setJson("json");
        instruction.setPublicKey("公钥");
        instruction.setInstructionId("id");
        instruction.setOldJson("old");
        instruction.setOperation(Operation.ADD);
        instruction.setTable("table");
        InstructionB instructionB=new InstructionB(instruction);

        String id= UUID.randomUUID().toString();
        dbStore.put(id.getBytes(),instructionB.encode());
        byte[] rs=dbStore.get(id.getBytes());
        InstructionB instructionB1=new InstructionB(rs);
        return instructionB1.convertToInstruction();
    }

    @RequestMapping("d")
    public Object d() throws UnsupportedEncodingException {
        Block block=new Block();
        BlockHeader blockHeader=new BlockHeader();
        BlockBody blockBody=new BlockBody();
        block.setBlockHeader(blockHeader);
        block.setBlockBody(blockBody);
        List<String> list=new ArrayList<>();
        list.add("1");
        list.add("2");
        List<Instruction> insList=new ArrayList();
        Instruction instruction=new Instruction();
        instruction.setHash("hash");
        insList.add(instruction);
        insList.add(instruction);
        block.getBlockHeader().setHashList(list);
        block.getBlockHeader().setHashMerkleRoot("HashMerkleRoot");
        block.getBlockHeader().setHashPreviousBlock("HashPreviousBlock");
        block.getBlockHeader().setNumber(123);
        block.getBlockHeader().setPublicKey("公钥");
        block.getBlockHeader().setTimeStamp(System.currentTimeMillis());
        block.getBlockHeader().setVersion(123);
        block.getBlockHeader().setNonce(999L);
        block.getBlockBody().setInstructions(insList);
        block.setHash("hash");

        BlockB blockB=new BlockB(block);
        String id= UUID.randomUUID().toString();
        dbStore.put(id.getBytes(),blockB.encode());
        byte[] rs=dbStore.get(id.getBytes());
        BlockB blockB1=new BlockB(rs);
        blockB1.setHash("has111".getBytes());
        return blockB1.convertToBlock();
    }
}
